<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/9/4
 * Time: 14:50
 */

namespace app\index\controller;
use think\Controller;
use feige\Feige;
use app\index\model\CrmManageCall;
use app\index\model\CrmZhongPuManageCall;
use app\index\model\CrmCall;
class FeiGeServer extends Controller
{
    //中普云通
    public function callback_xuexiang_url(){
        header('Content-Type:multipart/form-data');
        try{
            $data=input();
            if(empty($data['subId'])){
                $res = ['r'=>0];
                die(json_encode($res));
            }
            $zhong_pu_manage_call_model = new CrmZhongPuManageCall();
            if(!$zhong_pu_manage_call_model->where('subId',$data['subId'])->find()){
                express('callback_url', $data,'feige');
                $res = ['r'=>0];
                die(json_encode($res));
            }
           if(empty($data['result'])){
               $zhong_pu_manage_call_model->allowField('recording')->save($data,['subId'=>$data['subId']]);
               $res = ['r'=>0];
               die(json_encode($res));
           }
            if(isset($data['subId']) && isset($data['result']) && isset($data['duration'])){
                if($data['result']=='已接通' && $data['duration']>0){
                    $zhong_pu_manage_call_model->allowField(true)->save($data,['subId'=>$data['subId']]);
                }
            }
            $res = ['r'=>0];
            die(json_encode($res));
        }catch (\Exception $e){
            express('callback_url_error', $e->getMessage(),'feige');
        }
    }

    public function zhong_pu_dial($aNum,$bNum,$userData=''){
        $data=[
            'companyCode'=>'xuexiangjiaoyu',
            'xNum' => 'vdLQJ7',
            'aNum'=>$aNum,
            'bNum'=>$bNum,
            'userData'=>$userData
        ];
        $url = 'https://zpuyun.com/mycc/api/call/bind';
        try{
            $res = httpGet($url,'post',$data,["Content-Type:multipart/form-data"]);
            $result = json_decode($res,true);
            if($result['r']==0 && $result['ok']=='绑定成功'){
                return $result['subId'];
            }
            apiReturn(4000,'中普运通接口错误提示：',$result);
        }catch (\Exception $e){
            apiReturn(4000,$e->getMessage());
        }
    }







    //飞鸽
    public function voice_call($data){
        try{
            $mange['username']=$data['username'];
            $mange['name']=$data['name'];
            $customData = $jsonStr = json_encode($mange);
            $task = (new Feige())->voice_call($data['calledNumber'],$data['extensionNumber'],$customData);
            $task['manage_id']=$mange['username'];
            $task['extensionNumber']=$data['extensionNumber'];
            $task['calledNumber'] = $data['calledNumber'];
            $task['customData'] = $customData;
            $task['taskId']=$task['taskId']['taskId'];
            $task['callingNumber']=$data['callingNumber'];
            $task['call_id']=$data['call_id'];
            (new CrmManageCall)->allowField(true)->insert($task);
            return true;
        }catch (\Exception $e){
            apiReturn(4000,$e->getMessage());
        }
    }

    public function callback_url(){
        header('Content-type: application/json');
        try{
            $data = file_get_contents("php://input");
            $data = json_decode($data,true);
            $manage_call_m =  new CrmManageCall;
            $task = $manage_call_m->where('taskId',$data['taskId'])->find();
            if(empty($task)){
                $data['manage_id']='不是在系统中拨号的';
                $manage_call_m->allowField(true)->insert($data);die;
            }
//            $feige =  new Feige();
//            $task =  $task->toArray();
//            $feige_sign = $data['sign'];
//            unset($data['sign']);
//            $data=$data+$feige->common($task['nonce'], $task['timestamp']);
//            $sign = $feige->sign_data($data);
//            if($sign!=$feige_sign){
//                $data['manage_id']='校验失败';
//                $manage_call_m->allowField(true)->insert($data);
//            }else{
//                unset($data['customData']);
//                $manage_call_m->allowField(true)->save($data,['taskId'=>$data['taskId']]);
//            }
            $manage_call_m->allowField(true)->save($data,['taskId'=>$data['taskId']]);
        }catch (\Exception $e){
            express('callback_url', $e->getMessage(),'feige');
        }
    }


}